package com.eco.fanliapp.eureke.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Utils {
    
    private static final String ALGORIGTHM_MD5 = "MD5";
    private static final int CACHE_SIZE = 4096;
    
    public static String createMD5(String input) {
        try {
			return createMD5(input, "utf-8");
		} catch (Exception e) {
			return null;
		}
    }
    
    public static String createMD5(String input, String charset) throws Exception {
        byte[] data;
        if (charset != null && !"".equals(charset)) {
            data = input.getBytes(charset);
        } else {
            data = input.getBytes();
        }
        MessageDigest messageDigest = getMD5();
        messageDigest.update(data);
        return byteArrayToHexString(messageDigest.digest());
    }
    

    
    
    public static String generateFileMD5(String filePath) throws Exception {
        String md5 = "";
        File file = new File(filePath);
        if (file.exists()) {
            MessageDigest messageDigest = getMD5();
            InputStream in = new FileInputStream(file);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                messageDigest.update(cache, 0, nRead);
            }
            in.close();
            byte data[] = messageDigest.digest();
            md5 = byteArrayToHexString(data);
         }
        return md5;
    }
    
    private static String byteArrayToHexString(byte[] data) {
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 
        };
        char arr[] = new char[16 * 2];
        int k = 0;
        for (int i = 0; i < 16; i++) {
            byte b = data[i]; 
            arr[k++] = hexDigits[b >>> 4 & 0xf];
            arr[k++] = hexDigits[b & 0xf];
        }
        return new String(arr);
    }

    public static String MD5EncodeUtf8(String origin) {
        origin = origin +"geelysdafaqj23ou89ZXcj@#$@#$#@KJdjklj;D../dSF.,";
        return MD5Encode(origin, "utf-8");
    }
    /**
     * 返回大写MD5
     *
     * @param origin
     * @param charsetname
     * @return
     */
    private static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString.toUpperCase();
    }


    private static MessageDigest getMD5() throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(ALGORIGTHM_MD5);
    }
    
}